package com.mutual.controller.member;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mutual.base.controller.BaseController;
import com.mutual.base.form.MemberForm;
import com.mutual.entity.Member;
import com.mutual.service.IMemberServer;


@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {

	private Logger logger = Logger.getLogger(MemberController.class);
	
	@Resource
	private IMemberServer memberServer;
	
	@RequestMapping("/index.htm")
	public ModelAndView index(Member member){
		return new ModelAndView("member/member_list");
	}
	
	@RequestMapping("/list.htm")
	public void list(MemberForm form,HttpServletResponse response){
		try{
			memberServer.listMemberByPage(form.getPageResult(),form.getCondition());
			JSONObject root = toPageJson(form.getPageResult(), new String[]{"id","name","nickName","province","city","address","qq","email","mobile","password","twoPassword","regtime","loginTime","invokeCode","status"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询会员表时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}
	
	@RequestMapping("/disabledStatus.htm")
	public ModelAndView disabledStatus(MemberForm form){
		try {
			String selIds = form.getSelIds();
			memberServer.updateStatus(selIds,1);
			return super.ajaxJSON(Status.success);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("禁用出现错误："+e.getMessage(),e);
			return super.ajaxJSON(Status.error,"禁用出现错误！");
		}
	}
	
	@RequestMapping("/abledStatus.htm")
	public ModelAndView abledStatus(MemberForm form){
		try {
			String selIds = form.getSelIds();
			memberServer.updateStatus(selIds,0);
			return super.ajaxJSON(Status.success);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("启用出现错误："+e.getMessage(),e);
			return super.ajaxJSON(Status.error,"启用出现错误！");
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/checkMemberByCondition.htm")
	public void checkMemberByCondition(MemberForm form,HttpServletResponse response){
		Member item = form.getCondition();
		item.setAccount(item.getName());
		item.setName(null);
		List<Member> memberList = memberServer.listByCondition(item);
		JSONObject result = new JSONObject();
		if(CollectionUtils.isEmpty(memberList)){
			result.put("valid", true);
		}else{
			result.put("valid", false);
		}
		ajax(result, response);
	}
	
	@RequestMapping("/edit.htm")
	public ModelAndView edit(MemberForm form){
		ModelAndView mav = new ModelAndView("member/member_edit");
		return mav;
	}
	
	/**
	 * 保存会员表
	 * @return 
	 */
	@RequestMapping(value = "/save.htm")
	public ModelAndView saveMember(MemberForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isBlank(model.getCondition().getId())){
				memberServer.add(model.getCondition());
			}else{
				memberServer.update(model.getCondition());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存会员表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	
}
